
package com.cloud.rapid.user.controller;

import com.cloud.rapid.user.entity.SysUser;
import com.cloud.rapid.user.feign.RemoteProjectService;
import com.cloud.rapid.user.service.SysUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lengleng
 * @date 2019/2/1
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private RemoteProjectService remoteProjectService;

    /**
     * 初始化数据
     */
    @GetMapping(value = {"/init"})
    public String init() {
        //修改项目信息
        remoteProjectService.updateProject("1484aa83c695482a8dbcbe26cdfb6e6d", "初始化项目");
        //修改用户信息
        userService.updateUserById("26ff71ec665b4557b4fe64ff1f7d469d", "管理员");
        return "初始化成功";
    }

    /**
     * 测试事务回滚
     *
     * @param rollBack 是否要回滚
     * @return
     */
    @GlobalTransactional
    @GetMapping(value = {"/test-tx"})
    public List<SysUser> txTest(Boolean rollBack) {
        //修改项目信息
        remoteProjectService.updateProject("1484aa83c695482a8dbcbe26cdfb6e6d", "测试事务修改");
        //测试事务回滚
        if (rollBack) {
            throw new RuntimeException("测试事务回滚");
        }
        //修改用户信息
        userService.updateUserById("26ff71ec665b4557b4fe64ff1f7d469d", "事务修改");
        List<SysUser> data = userService.getUserInfo();
        return data;
    }
}
